using Microsoft.Extensions.DependencyInjection;
using ZeroBlog.Application.Categories.Dtos.In;
using ZeroBlog.Application.Categories.Dtos.Out;
using ZeroBlog.Application.Categories.Dtos.Query;
using ZeroBlog.Core.Domains.Blog;
using ZeroBlog.Core.DomainServices.Categories;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.Extend.Mapper.Extensions;
using ZeroSum.Models;

namespace ZeroBlog.Application.Categories;

[Register(ServiceLifetime.Scoped)]
public class CategoryApplicationService : ICategoryApplicationService
{
    private readonly ICategoryDomainService _domainService;

    public CategoryApplicationService(ICategoryDomainService domainService)
    {
        _domainService = domainService;
    }

    public async Task AddAsync(AddCategoryDto dto)
    {
        var domain = dto.MapTo<Category>();
        await _domainService.AddAsync(domain);
    }

    public async Task DeleteAsync(DeleteCategoryDto dto)
    {
        await _domainService.DeleteAsync(dto.Ids);
    }

    public async Task UpdateAsync(UpdateCategoryDto dto)
    {
        var domain = dto.MapTo<Category>();
        await _domainService.UpdateAsync(domain);
    }

    public async Task<DetailCategoryDto> Detail(DetailCategoryQuery query)
    {
        var domain = await _domainService.GetAsync(x => x.Id == query.Id);
        return domain.MapTo<DetailCategoryDto>();
    }

    public async Task<PagedResponseModel<PageCategoryDto>> Page(PageCategoryQuery query)
    {
        var (domains, count) = await _domainService.Query(query.Keyword, query.PageNumber, query.PageSize);
        var list = domains.MapTo<List<PageCategoryDto>>();
        return new PagedResponseModel<PageCategoryDto>(query.PageNumber, query.PageSize, count, list);
    }
}